package com.pharmacy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pharmacy.constant.MessageConstant;
import com.pharmacy.constant.PasswordConstant;
import com.pharmacy.constant.StatusConstant;
import com.pharmacy.dto.*;
import com.pharmacy.entity.Employees;

import com.pharmacy.exception.AccountException;
import com.pharmacy.mapper.EmployeesMapper;
import com.pharmacy.result.PageResult;
import com.pharmacy.service.EmployeesService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;


@Service
public class EmployeesServiceImlp implements EmployeesService {


    @Autowired
    private EmployeesMapper employeesMapper;


    /**
     * 员工登录
     * @param employeesLoginDto
     * @return
     */
    @Override
    public Employees login(EmployeesLoginDto employeesLoginDto) {

        String username = employeesLoginDto.getUsername();
        String password = DigestUtils.md5DigestAsHex(employeesLoginDto.getPassword().getBytes());
        Employees employees = employeesMapper.login(username);

        if(employees == null){
            throw new AccountException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!password.equals(employees.getPassword())){
            throw new AccountException(MessageConstant.PASSWORD_ERROR);
        }
        if (employees.getStatus()==0){
            throw new AccountException(MessageConstant.ACCOUNT_LOCKED);
        }


        return employees;
    }

    /**
     * 新增员工
     * @param employeesAddDto
     */
    @Override
    public void addEmployees(EmployeesAddDto employeesAddDto) {
        Employees employees = new Employees();
        BeanUtils.copyProperties(employeesAddDto,employees);
        employees.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.PASSWORD.getBytes()));
        System.out.println("加密过的密码:"+employees.getPassword());
        employees.setStatus(StatusConstant.ENABLE);
//        employees.setCreatedBy(BaseContext.getCurrentId());
//        employees.setUpdatedBy(BaseContext.getCurrentId());
//        employees.setCreatedAt(LocalDateTime.now());
//        employees.setUpdatedAt(LocalDateTime.now());//

        employeesMapper.addEmployees(employees);
    }

    /**
     * 删除员工
     * @param id
     */
    @Override
    public void deleteEmployees(Long id) {
        employeesMapper.deleteEmployees(id);
    }

    /**
     * 修改员工信息
     * @param employeesUpdateDto
     */
    @Override
    public void updateEmployees(EmployeesUpdateDto employeesUpdateDto) {
        Employees employees = new Employees();
        BeanUtils.copyProperties(employeesUpdateDto,employees);
        employeesMapper.updateEmployees(employees);
    }

    /**
     * 员工分页查询
     * @param employeesPageSelectDto
     * @return
     */
    @Override
    public PageResult pageSelect(EmployeesPageSelectDto employeesPageSelectDto) {

        PageHelper.startPage(employeesPageSelectDto.getPage(),employeesPageSelectDto.getPageSize());
        Page<Employees> page = employeesMapper.pageEmployeesSelect(employeesPageSelectDto);
        long total = page.getTotal();
        List<Employees> records = page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 修改密码
     * @param updatePasswordDto
     */
    @Transactional
    @Override
    public void updatePassword(UpdatePasswordDto updatePasswordDto) {
        Employees employees1 = new Employees();
        String username = updatePasswordDto.getUsername();
        String password = DigestUtils.md5DigestAsHex(updatePasswordDto.getPassword().getBytes());
        String newPassword = DigestUtils.md5DigestAsHex(updatePasswordDto.getNewPassword().getBytes());
        Employees employees = employeesMapper.login(username);
        if (employees == null){
            throw new AccountException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!password.equals(employees.getPassword())){
            throw new AccountException(MessageConstant.PASSWORD_ERROR);
        }
        password = newPassword;
        employees1.setId(employees.getId());
        employees1.setPassword(password);
        employeesMapper.updateEmployees(employees1);
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public Employees getById(Integer id) {
        Employees employees = employeesMapper.getById(id);
        return employees;
    }


}
